using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Environment = System.Environment;

namespace WisdriContactsAndroid
{
	[Application]
	public class MainApp:Application
	{
		public MainApp(IntPtr javaReference, JniHandleOwnership transfer)
			: base(javaReference, transfer)
		{
		}

		public override void OnCreate()
		{
			base.OnCreate();
			var docFolder = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
			var dbFile = Path.Combine(docFolder, "contacts.db"); // FILE NAME TO USE WHEN COPIED
			if (!System.IO.File.Exists(dbFile))
			{
				var s = Resources.OpenRawResource(Resource.Raw.contacts);  // DATA FILE RESOURCE ID
				FileStream writeStream = new FileStream(dbFile, FileMode.OpenOrCreate, FileAccess.Write);
				ReadWriteStream(s, writeStream);
			}
		}
		// readStream is the stream you need to read
		// writeStream is the stream you want to write to
		private void ReadWriteStream(Stream readStream, Stream writeStream)
		{
			int Length = 256;
			Byte[] buffer = new Byte[Length];
			int bytesRead = readStream.Read(buffer, 0, Length);
			// write the required bytes
			while (bytesRead > 0)
			{
				writeStream.Write(buffer, 0, bytesRead);
				bytesRead = readStream.Read(buffer, 0, Length);
			}
			readStream.Close();
			writeStream.Close();
		}
	}
}

